package com.polaris.server.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.polaris.server.dto.PassengerAgeStruct;
import com.polaris.server.dto.PassengerQueryParam;
import com.polaris.server.pojo.TrafficPassenger;
import com.polaris.server.mapper.TrafficPassengerMapper;
import com.polaris.server.service.ITrafficPassengerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author polaris
 * @since 2021-06-02
 */
@Service
public class TrafficPassengerServiceImpl extends ServiceImpl<TrafficPassengerMapper, TrafficPassenger> implements ITrafficPassengerService {

    private final TrafficPassengerMapper passengerMapper;

    public TrafficPassengerServiceImpl(TrafficPassengerMapper passengerMapper) {
        this.passengerMapper = passengerMapper;
    }

    /**
     * 获取乘客的年龄结构
     *
     * @return
     */
    @Override
    public List<PassengerAgeStruct> passengerAgeStruct() {
        try{
            QueryWrapper<TrafficPassenger> queryWrapper = new QueryWrapper<>();
            Date date = DateUtil.date();
            int year = DateUtil.year(date);
            ArrayList<PassengerAgeStruct> ageStructList = new ArrayList<>();
            List<String> ageStrList = ListUtil.toLinkedList("0-6", "7-17", "18-40", "41-65", "66+");
            List<Integer> ageIntervalList = ListUtil.toLinkedList(0,6,7,17,18,40,41,65);

            int count=0;
            for (int i = 0; i < ageIntervalList.size(); i++) {
                // 66岁以下
                queryWrapper.clear();
                queryWrapper.le("birth",year-ageIntervalList.get(i)).ge("birth",year-ageIntervalList.get(++i));
                Integer countUnder66 = passengerMapper.selectCount(queryWrapper);
                PassengerAgeStruct passengerUnder66 = new PassengerAgeStruct();
                passengerUnder66.setName(ageStrList.get(count++));
                passengerUnder66.setValue(countUnder66);
                ageStructList.add(passengerUnder66);
            }
            // 66岁以上
            queryWrapper.clear();
            queryWrapper.ge("birth",66);
            Integer countAbove66 = passengerMapper.selectCount(queryWrapper);
            PassengerAgeStruct passengerAbove66 = new PassengerAgeStruct();
            passengerAbove66.setName(ageStrList.get(count));
            passengerAbove66.setValue(countAbove66);
            ageStructList.add(passengerAbove66);

            return ageStructList;
        } catch (Exception e){
            log.error(e.getMessage());
        }
        return null;
    }

    /**
     * 乘客总数
     *
     * @return
     */
    @Override
    public Integer countNormal() {
        return passengerMapper.selectCount(new QueryWrapper<>());
    }

    /**
     * 根据性别统计乘客数量
     *
     * @param gender
     * @return
     */
    @Override
    public Integer countByGender(Integer gender) {
        return passengerMapper.selectCount(new QueryWrapper<TrafficPassenger>().eq("gender",gender));
    }

    /**
     * 统计不同性别的乘客数量
     *
     * @return
     */
    @Override
    public Map<String, Object> countByGender() {
        HashMap<String, Object> resultMap = new HashMap<>();
        QueryWrapper<TrafficPassenger> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("gender",1);
        resultMap.put("man",passengerMapper.selectCount(queryWrapper));
        queryWrapper.clear();
        queryWrapper.eq("gender",0);
        resultMap.put("woman",passengerMapper.selectCount(queryWrapper));
        return resultMap;
    }

    /**
     * 根据省份统计乘客数量
     *
     * @param dist
     * @return
     */
    @Override
    public Integer countByDist(Integer dist) {
        return passengerMapper.selectCount(new QueryWrapper<TrafficPassenger>().eq("dist",dist));
    }

    /**
     * 获取passenger list
     *
     * @param queryParam
     * @param page
     * @return
     */
    @Override
    public IPage<TrafficPassenger> list(PassengerQueryParam queryParam, Page<TrafficPassenger> page) {
        if (StringUtils.hasText(queryParam.toString())){
            QueryWrapper<TrafficPassenger> queryWrapper = new QueryWrapper<>();
            Map<String, Object> map = BeanUtil.beanToMap(queryParam,true,false);
            queryWrapper.allEq(map,false);
            return passengerMapper.selectPage(page, queryWrapper);
        }
        return null;
    }

    /**
     * 通过gender获取乘客list
     *
     * @param gender
     * @param page
     * @return
     */
    @Override
    public IPage<TrafficPassenger> listByGender(Integer gender, Page<TrafficPassenger> page) {
        return passengerMapper.selectPage(page, new QueryWrapper<TrafficPassenger>().eq("gender", gender));
    }

    /**
     * 通过dist获取乘客list
     *
     * @param dist
     * @param page
     * @return
     */
    @Override
    public IPage<TrafficPassenger> listByDist(Integer dist, Page<TrafficPassenger> page) {
        return passengerMapper.selectPage(page, new QueryWrapper<TrafficPassenger>().eq("dist", dist));
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @Override
    public TrafficPassenger getById(Long id) {
        return passengerMapper.selectById(id);
    }

    /**
     * 通过乘客id查询
     *
     * @param pid
     * @return
     */
    @Override
    public TrafficPassenger getByPassengerId(String pid) {
        return passengerMapper.selectOne(new QueryWrapper<TrafficPassenger>().eq("passenger_id",pid));
    }

    /**
     * 添加乘客
     *
     * @param queryParam
     * @return
     */
    @Override
    public TrafficPassenger createPassenger(PassengerQueryParam queryParam) {
        try {
            TrafficPassenger passenger = new TrafficPassenger();
            BeanUtils.copyProperties(queryParam, passenger);
            QueryWrapper<TrafficPassenger> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("passenger_id", passenger.getPassengerId());
            if(passengerMapper.selectCount(queryWrapper)>0){
                return null;
            }
            passengerMapper.insert(passenger);
            return passenger;
        }
        catch (Exception e){
            log.error(e.getMessage());
        }
        return null;
    }

    /**
     * 更新乘客信息
     *
     * @param queryParam
     * @return
     */
    @Override
    public TrafficPassenger updatePassenger(PassengerQueryParam queryParam) {

        try{
            TrafficPassenger passenger = getById(queryParam.getId());
            if(passenger==null){
                return null;
            }
            passenger.setBirth(queryParam.getBirth());
            passenger.setDist(queryParam.getDist());
            passenger.setGender(queryParam.getGender());
            passengerMapper.updateById(passenger);
            return passenger;
        }
        catch (Exception e){
            log.error(e.getMessage());
        }
        return null;
    }

    /**
     * 通过id删除乘客
     *
     * @param id
     * @return
     */
    @Override
    public Boolean deleteById(Long id) {
        try {
            passengerMapper.deleteById(id);
            return true;
        }
        catch (Exception e){
            log.error(e.getMessage());
        }
        return false;
    }

    /**
     * 通过pid删除乘客
     *
     * @param pid
     * @return
     */
    @Override
    public Boolean deleteByPassengerId(String pid) {
        try{
            passengerMapper.delete(new QueryWrapper<TrafficPassenger>().eq("passenger_id", pid));
            return true;
        }
        catch (Exception e){
            log.error(e.getMessage());
        }
        return false;
    }
}
